<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px; width: 100%"
  >
    <el-col :span="20">
      <span>{{ nodeData.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ nodeData.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown @command="handleCommand">
            <span @click.stop> 操作<i class="el-icon-arrow-down" /> </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item
                v-if="!isRoot"
                command="edit"
              >编辑部分</el-dropdown-item>
              <el-dropdown-item
                v-if="!isRoot"
                command="del"
              >删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { reqDelDepartment } from '@/api/department'
export default {
  props: {
    nodeData: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    async handleCommand(command) {
      if (command === 'add') {
        this.$emit('showAddDialog', this.nodeData)
      }
      if (command === 'edit') {
        this.$emit('showEditDialog', this.nodeData)
      }
      if (command === 'del') {
        // 1. 确认消息提示
        const str = await this.$confirm('您是否要删除此部门?', '删除提示', {
          confirmButtonText: '是',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(() => {
          this.$message.info('已取消操作')
        })
        // 2. str是confirm是点击了确认
        if (str === 'confirm') {
          // 3. 发送请求
          const { message } = await reqDelDepartment(this.nodeData.id)
          // 4. 提示用户
          this.$message.success(message)
          // 5. 通知父组件 重新渲染部门列表
          this.$emit('reload')
        }
      }
    }
  }
}
</script>

<style>
</style>
